package org.bbelovic.devel.pexeso.gui;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.swing.JOptionPane;

/**
 * Utility class with various helper methods used in Pairs game.
 */
public class Utilities {

    private static final String FILE_PREFIX = "file:";
    private static final int FILE_PREFIX_LENGTH = FILE_PREFIX.length();
    private static final String JAR_PREFIX = "jar:";
    private static final int JAR_PREFIX_LENGTH = JAR_PREFIX.length();

    private Utilities() {
        // no instances allowed
    }

    public static String getRootPathAsString() {
        // need to remove leading file: string
        return Utilities.class.getResource("/images").getFile();
    }

    /**
     * Returns path for given resource (on classpath or inside a jar) as a string. Leading file:// prefix is
     * cut off.
     * @param resource resource name
     * @return path to resource converted to string
     */
    public static String getResourcePathAsString(final String resource) {
        final String result = Utilities.class.getResource(resource).toString();
        if (result != null && result.startsWith(JAR_PREFIX + FILE_PREFIX)) {
            return result.substring(JAR_PREFIX_LENGTH + FILE_PREFIX_LENGTH);
        }
        return result.substring(FILE_PREFIX_LENGTH);
    }
    
    public static List<String> getImagePaths() {
        final List<String> imagePaths = new ArrayList<String>();
        final BufferedReader br = new BufferedReader(new InputStreamReader(Utilities.class.getResourceAsStream("/images/toc.txt")));
        String line = null;
        try {
            while ((line = br.readLine()) != null) {
                imagePaths.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imagePaths;
    }
    
    public static Set<String> getResources(String resourcePath) {
        if (resourcePath.contains("!")) {
            return getResourcesFromJar(resourcePath);
        } else {
            return getResourcesFromFile(resourcePath);
        }
    }
    
    public static Set<String> getResourcesFromJar(String path) {
        final Set<String> names = new HashSet<String>();
        if (path.indexOf("!") != -1) {
            path = path.substring(0, path.indexOf("!"));
                try {
                    final JarFile jar = new JarFile(path);
                    final Enumeration<JarEntry> entries = jar.entries();
                    while (entries.hasMoreElements()) {
                        JarEntry entry = entries.nextElement();
                        names.add(entry.getName());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        return names;
    }
    
    private static Set<String> getResourcesFromFile(String path) {
        final Set<String> names = new HashSet<String>();
        final String [] fileNames = new File(path).list();
        for (final String eachName: fileNames) {
            names.add(eachName);
        }
        return names;
    }
    
    /**
     * Returns true if string passed as argument is not <code>null</code> and has non-zero length.
     * @param inputString string to be checked
     * @return true if input string is not <code>null</code> and has non-zero length
     */
    public static boolean notEmpty(final String inputString) {
        return (inputString != null && !inputString.isEmpty());
    }

    /**
     * Displays error message dialog with specified message and title.
     * @param title title of an error message dialog
     * @param message a message to be displayed
     */
    public static void showErrorMessage(final String title, final String message) {
        JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
    }
}
